package com.msg.test.model;

import com.msg.test.util.MsgUtil;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;

/**
 * Created by Administrator on 2017/10/19 0019.
 */
public class MsgSubmit extends MsgHead{
    private static Log logger= LogFactory.getLog(MsgSubmit.class);
    private long msgId = 0;
    private byte pkTotal = 0x01;
    private byte pkNumber = 0x01;
    private byte registeredDelivery = 0x00; //是否要求返回状态确认报告
    private byte msgLevel = 0x01; //信息级别
    private String serviceId = ""; //业务类型
    private byte feeUserType = 0x00;// 计费用户类型
    private String feeTerminalId = ""; //被计费用户号码
    //	private byte feeTerminalType = 0x00;
    private byte tpPId = 0x00; //GSM协议类型
    private byte tpUdhi = 0x00;//GSM协议类型
    private byte msgFmt = 0x0f;
    private String msgSrc;
    /**
     * 01：对“计费用户号码”免费
     * 02：对“计费用户号码”按条计信息费
     * 03：对“计费用户号码”按包月收取信息费
     * 04：对“计费用户号码”的信息费封顶
     * 05：对“计费用户号码”的收费是由SP实现
     */
    private String feeType = "01";//资费类别
    private String feeCode = "";//资费代码
    private String valIdTime = "";// 暂不支持
    private String atTime = "";// 暂不支持
    // SP的服务代码或前缀为服务代码的长号码,
    // 网关将该号码完整的填到SMPP协议Submit_SM消息相应的source_addr字段，该号码最终在用户手机上显示为短消息的主叫号码。
    private String srcId;
    private byte destUsrTl = 0x01;// 不支持群发
    private String destTerminalId;// 接收手机号码，
    //	private byte destTerminalType = 0x00;// 真实号码
    private byte msgLength;
    private byte[] msgContent;
    // 点播业务使用的LinkID，非点播类业务的MT流程不使用该字段
    private String reserve = "00000000";// 保留字段

    public byte[] toByteArray(){
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        DataOutputStream dos = new DataOutputStream(baos);
        try {
            dos.writeInt(this.getTotalLength());
            dos.write(this.getCommandId());
            dos.writeInt(this.getSequence());
            dos.writeLong(this.msgId);//信息标示，由SP接入的短信网关本身产生，本处填空
            dos.writeByte(this.pkTotal);//相同msgid信息总条数
            dos.writeByte(this.pkNumber);//相同msgid信息序号从1开始
            dos.writeByte(this.registeredDelivery);//是否要求返回状态确认报告
            dos.writeByte(this.msgLevel);//信息级别
            MsgUtil.writeString(dos,this.serviceId,10);
            dos.writeByte(this.feeUserType);
            MsgUtil.writeString(dos,this.feeTerminalId,21);
            dos.writeByte(this.tpPId);
            dos.writeByte(this.tpUdhi);
            dos.writeByte(this.msgFmt);
            MsgUtil.writeString(dos,this.msgSrc,6);//Msg_src 信息内容来源(SP_Id)
            MsgUtil.writeString(dos,this.feeType,2);//FeeType 资费类别
            MsgUtil.writeString(dos,this.feeCode,6);//FeeCode
            MsgUtil.writeString(dos,this.valIdTime,17);//存活有效期
            MsgUtil.writeString(dos,this.atTime,17);//定时发送时间
            MsgUtil.writeString(dos,this.srcId,21);//Src_Id spCode
            dos.writeByte(this.destUsrTl);//DestUsr_tl
            MsgUtil.writeString(dos,this.destTerminalId,21);//Dest_terminal_Id
//			dous.writeByte(this.destTerminalType);//Dest_terminal_type 接收短信的用户的号码类型，0：真实号码；1：伪码
            dos.writeByte(this.msgLength);//Msg_Length
            dos.write(this.msgContent);//信息内容
            MsgUtil.writeString(dos, this.reserve, 8);// 保留
            dos.close();
        }catch (Exception ex){
            logger.error("封装短信发送二进制数组失败。");
        }
        return baos.toByteArray();
    }

    public long getMsgId() {
        return msgId;
    }
    public void setMsgId(long msgId) {
        this.msgId = msgId;
    }
    public byte getPkTotal() {
        return pkTotal;
    }
    public void setPkTotal(byte pkTotal) {
        this.pkTotal = pkTotal;
    }
    public byte getPkNumber() {
        return pkNumber;
    }
    public void setPkNumber(byte pkNumber) {
        this.pkNumber = pkNumber;
    }
    public byte getRegisteredDelivery() {
        return registeredDelivery;
    }
    public void setRegisteredDelivery(byte registeredDelivery) {
        this.registeredDelivery = registeredDelivery;
    }
    public byte getMsgLevel() {
        return msgLevel;
    }
    public void setMsgLevel(byte msgLevel) {
        this.msgLevel = msgLevel;
    }
    public String getServiceId() {
        return serviceId;
    }
    public void setServiceId(String serviceId) {
        this.serviceId = serviceId;
    }
    public byte getFeeUserType() {
        return feeUserType;
    }
    public void setFeeUserType(byte feeUserType) {
        this.feeUserType = feeUserType;
    }
    public String getFeeTerminalId() {
        return feeTerminalId;
    }
    public void setFeeTerminalId(String feeTerminalId) {
        this.feeTerminalId = feeTerminalId;
    }
    public byte getTpPId() {
        return tpPId;
    }
    public void setTpPId(byte tpPId) {
        this.tpPId = tpPId;
    }
    public byte getTpUdhi() {
        return tpUdhi;
    }
    public void setTpUdhi(byte tpUdhi) {
        this.tpUdhi = tpUdhi;
    }
    public byte getMsgFmt() {
        return msgFmt;
    }
    public void setMsgFmt(byte msgFmt) {
        this.msgFmt = msgFmt;
    }
    public String getMsgSrc() {
        return msgSrc;
    }
    public void setMsgSrc(String msgSrc) {
        this.msgSrc = msgSrc;
    }
    public String getFeeType() {
        return feeType;
    }
    public void setFeeType(String feeType) {
        this.feeType = feeType;
    }
    public String getFeeCode() {
        return feeCode;
    }
    public void setFeeCode(String feeCode) {
        this.feeCode = feeCode;
    }
    public String getValIdTime() {
        return valIdTime;
    }
    public void setValIdTime(String valIdTime) {
        this.valIdTime = valIdTime;
    }
    public String getAtTime() {
        return atTime;
    }
    public void setAtTime(String atTime) {
        this.atTime = atTime;
    }
    public String getSrcId() {
        return srcId;
    }
    public void setSrcId(String srcId) {
        this.srcId = srcId;
    }
    public byte getDestUsrTl() {
        return destUsrTl;
    }
    public void setDestUsrTl(byte destUsrTl) {
        this.destUsrTl = destUsrTl;
    }
    public String getDestTerminalId() {
        return destTerminalId;
    }
    public void setDestTerminalId(String destTerminalId) {
        this.destTerminalId = destTerminalId;
    }
    public byte getMsgLength() {
        return msgLength;
    }
    public void setMsgLength(byte msgLength) {
        this.msgLength = msgLength;
    }
    public byte[] getMsgContent() {
        return msgContent;
    }
    public void setMsgContent(byte[] msgContent) {
        this.msgContent = msgContent;
    }
    public String getReserve() {
        return reserve;
    }
    public void setReserve(String reserve) {
        this.reserve = reserve;
    }

}
